package com.wzdigit.wms.wms.task;

import com.wzdigit.mes.basic.api.BasicParamApi;
import com.wzdigit.mes.basic.api.BasicSimpleCodeRuleApi;
import com.wzdigit.wms.basic.mapper.core.inventory.LotnoMapper;
import com.wzdigit.wms.basic.mapper.core.oqc.OqcInspDtlMapper;
import com.wzdigit.wms.basic.mapper.core.oqc.OqcInspMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlGoodsMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlMapper;
import com.wzdigit.wms.wms.service.core.GoodsService;
import com.wzdigit.wms.basic.service.code.InventoryService;
import com.wzdigit.wms.wms.service.order.TrsnService;
import com.wzdigit.wms.wms.service.wms2sap.ReportCheckService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName OqcTask
 * @Description 定时任务-产品判检
 * @Author LHR-PC
 * @Date 2021/1/25 9:14
 * @Version
 **/
/*@ConditionalOnExpression("${schedule.common.switch}")*/
@Component
@Slf4j
public class OqcTask {

    @Autowired
    private OqcInspMapper oqcInspMapper;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private OqcInspDtlMapper oqcInspDtlMapper;

    @Autowired
    private ReportCheckService reportCheckService;

    @Autowired
    private TrsnService trsnService;

    @Autowired
    private AsnDtlMapper asnDtlMapper;

    @Autowired
    private AsnDtlGoodsMapper asnDtlGoodsMapper;

    @Autowired
    private LotnoMapper lotnoMapper;

    @Reference(version = "${dubbo.consumers.basic.simple.code.rule.api:1.0.0}", check = false)
    private BasicSimpleCodeRuleApi basicSimpleCodeRuleApi;
    
    @Reference(version = "${dubbo.providers.basic.BasicParam.api:1.0.0}", check = false)
    private BasicParamApi basicParamApi;

    private static final String NOTE_NO = "TRSN_ORDER";

    /**
     * 后台定时任务，每5分钟处理一次，
     * 定时获取 OQC送检单(WMS_OQC_INSP) 状态(STATUS)等于 未处理(0)，部分处理(1)的送检单，
     * 找到对应OQC送检单明细(WMS_OQC_INSP_DTL)状态是“未处理”的产品SN明细列表。
     * 根据SN列表，查询货品清单(WMS_GOODS)表，如果没有则不允许翻牌，
     * 获取SN的状态(STATUS)，状态等于 新建(0)、转出在途(10)不允许翻牌，其他情况允许翻牌。
     */
    /*@Scheduled(cron = "0 0/5 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public synchronized void oqcTaskNew() {
        // 获取 OQC送检单(WMS_OQC_INSP) 状态(STATUS)等于 未处理(0)，部分处理(1)的送检单（未过滤SN），
        List<OqcInspDto> oqcInspDtosNotFilter = oqcInspMapper.selectOqcTaskData();
    	List<OqcInspDto> oqcInspDtosNotFilter = null;
        if (CollectionUtil.isEmpty(oqcInspDtosNotFilter)) {
            log.info("【定时任务-产品判检】未处理或部分处理的送检单数量：0");
            return;
        }
        // 所有明细行ID，未过滤SN
        List<Integer> idsAll = oqcInspDtosNotFilter.stream().map(o -> o.getOqcInspId()).collect(Collectors.toList());
        // 记录有明细不符合翻牌条件的主档ID(部分处理的主档ID)
        List<Integer> idsCannotFlop = new ArrayList<>();
        int beContinue = 0;
        List<OqcInspDto> oqcInspDtoListBk = new ArrayList<>();
//        oqcInspDtoListBk.addAll(oqcInspDtosNotFilter);
//        SN存在，可翻牌的
        for (OqcInspDto oqcInspDto : oqcInspDtosNotFilter) {
//            log.info("----------------------------------------是这里吗1-------------------------------------");
            Goods goods = goodsService.selectBySn(oqcInspDto.getGoodsSn());
            if (goods!=null){
                oqcInspDtoListBk.add(oqcInspDto);
            }
        }

        // 不允许翻拍的记录
        List<OqcInspDto> oqcInspDtos = oqcInspDtoListBk.stream().filter(new Predicate<OqcInspDto>() {
            @Override
            public boolean test(OqcInspDto oqcInspDto) {
                //2021/03/15 如果找不到仓库，则跳过这一条，别的继续翻牌
//                log.info("----------------------------------------是这里吗2-------------------------------------");
                Goods goods = goodsService.selectBySn(oqcInspDto.getGoodsSn());
                AsnDtlGoods asnDtlGoods = asnDtlGoodsMapper.selectAsnTypeIsR01BySn(goods.getGoodsSn());

                if (goods.getGoodsId() == null || GoodsStatusEnum.CREATE.getCode() == goods.getStatus()
                        || GoodsStatusEnum.OUT_ONLOAD.getCode() == goods.getStatus() || goods.getWhsId() == null
                        //2021.04.10 可翻牌的条码中包含了‘R98’，改为只翻‘R01’类型的条码
                        || asnDtlGoods == null
                ) {
//                    log.info("条码{}", oqcInspDto.getGoodsSn());
                    return false;
                }
                return true;
            }
        }).collect(Collectors.toList());


        //不可翻牌的明细
        List<OqcInspDto> oqcInspDtosCannotFlop = oqcInspDtosNotFilter.stream().filter(o -> !oqcInspDtos.contains(o)).collect(Collectors.toList());
        //不可翻牌的主档ID
        idsCannotFlop = oqcInspDtosCannotFlop.stream().map(o -> o.getOqcInspId()).collect(Collectors.toList());

        int rowIndex = 1;
        for (OqcInspDto oqcInspDto : oqcInspDtos) {

            log.info("【定时任务-产品判检】,送检单号：{}", oqcInspDto.getInspNo());

//            log.info("----------------------------------------是这里吗3-------------------------------------");
            Goods goods = goodsService.selectBySn(oqcInspDto.getGoodsSn());
//            if (goods == null || goods.getGoodsId() == null || GoodsStatusEnum.CREATE.getCode() == goods.getStatus()
//                    || GoodsStatusEnum.OUT_ONLOAD.getCode() == goods.getStatus()) {
//                ids.add(oqcInspDto.getOqcInspId());
//                beContinue++;
//                oqcInspDtos.remove(oqcInspDto);
//                continue;
//            }

            oqcInspDto.setQaStatusOld(goods.getQaStatus()); //记录下旧的品质状态
            oqcInspDto.setLocId(goods.getLocId());
            oqcInspDto.setQty(goods.getQty());
            oqcInspDto.setOwner(goods.getOwner());
            oqcInspDto.setSkuId(goods.getSkuId());
            oqcInspDto.setFactory(goods.getFactory());
            oqcInspDto.setVendorId(goods.getVendorId());
            oqcInspDto.setVenLotno(goods.getVenLotno());
            oqcInspDto.setInvAttr(goods.getInvAttr());
            oqcInspDto.setQaStatus(goods.getQaStatus());
            oqcInspDto.setSnStatus(goods.getStatus());
            oqcInspDto.setGoodsId(goods.getGoodsId());
            oqcInspDto.setWhsId(goods.getWhsId());
            oqcInspDto.setLotno(goods.getLotno());
            oqcInspDto.setUnit(goods.getUnit());

            try {
                //扣减库存

                UpdateInventoryInputDto old = new UpdateInventoryInputDto();
                old.setLocId(oqcInspDto.getLocId());//设置库位
                old.setQty(oqcInspDto.getQty().negate());
                old.setPreInQty(BigDecimal.ZERO);
                old.setPreOutQty(BigDecimal.ZERO);
                old.setHoldQty(BigDecimal.ZERO);
                old.setAlocQty(BigDecimal.ZERO);
                old.setPreAlocQty(BigDecimal.ZERO);
                old.setOperator("system");
                old.setWhsId(oqcInspDto.getWhsId());
                old.setOwner(oqcInspDto.getOwner());
                old.setSkuId(oqcInspDto.getSkuId());
                old.setFactory(oqcInspDto.getFactory());
                old.setVendorId(oqcInspDto.getVendorId());
                old.setLotNo(oqcInspDto.getLotno());
                old.setInvAttr(oqcInspDto.getInvAttr());
                old.setQaStatus(oqcInspDto.getQaStatusOld());
                old.setUnit(oqcInspDto.getUnit());
                InvTrans log = new InvTrans();
                log.setOpCode(OpCodeEnum.INSPECTIONRESULT.getDescription());
                log.setDocType(" ");//TODO
                log.setDocNo(" ");
                log.setDocLineNo(" ");
                log.setTrsnNo(" ");
                log.setTrsnLineNo(String.valueOf(" "));
                log.setStatus(InvTransStatusEnum.FINISHED.getCode());
                log.setTransTime(new Date());
                log.setOperator("system");
                log.setTransQty(old.getQty());
                log.setCreateTime(new Date());
                log.setCreateUser("system");
                if (old.getWhsId() != null) {
                    inventoryService.inventoryOut(old, log);
                }
            } catch (Exception e) {
                log.info("【定时任务-产品判检】翻牌-扣库存失败{}", oqcInspDto.getInspNo());
                e.printStackTrace();
                throw e;
            }
            //	插入库存交易凭证(WMS_TRSN_HEADER)、库存交易记录明细(WMS_TRSN_DTL)记录, WMS_TRSN_DTL_GOODS
            try {
                rowIndex = insertTrsn(rowIndex, oqcInspDto, -1);
            } catch (Exception e) {
                e.printStackTrace();
                log.warn("【定时任务-产品判检】库存交易凭证：出库");
                log.warn(e.getMessage() + oqcInspDto.getInspNo());
            }

            //	变更货品清单(WMS_GOODS)的品质状态，待检—>合格/不合格；合格—>不合格；不合格—>合格。调【更新货品】、【生成SN操作日志】服务
            goods.setQaStatus(oqcInspDto.getInspResult());
            oqcInspDto.setQaStatus(oqcInspDto.getInspResult());
//            Goods goodsRecord = goodsService.selectBySn(oqcInspDto.getGoodsSn()); //为了记录旧的goods日志，再查一次
            goodsService.updateGoods(null, goods);
            GoodsLog goodsLog = new GoodsLog();
            Goods oldGoods = new Goods();
            BeanUtil.copyNonNull(goods, oldGoods);
            oldGoods.setQaStatus(oqcInspDto.getQaStatusOld());
            goodsLog.setGoodsSn(goods.getGoodsSn());
            goodsLog.setOpCode("翻牌" + goods.getGoodsSn());
            goodsLog.setReason("变更品质状态");
            goodsLog.setOperator("定时任务-产品判检");
            goodsLog.setUpdateContent("未更新的：" + JSON.toJSONString(oldGoods) + ",更新后的：" + JSON.toJSONString(goods));
            goodsLog.setCreateUser("system");
            goodsLog.setCreateTime(new Date());
            goodsService.insertGoodsLog(goodsLog);

        }


        if (oqcInspDtos.size() == beContinue) {
            log.info("【定时任务-产品判检】没有状态不等于0（新建）或10（转出在途)的goods记录");
            return;
        }
//        仓库、库位、货主、货品、工厂、供应商、批次号、库存性质、品质状态”分组统计数量
        Map<String, List<OqcInspDto>> collect = null;
        try {
            collect = oqcInspDtos.stream()
                    .collect(Collectors.groupingBy(p -> spliceGroupingKey(p)));
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            throw e;
        }
        Set<String> keySet = null;
        try {
            keySet = collect.keySet();
        } catch (Exception e) {
            log.error("分组统计数量为空");
            e.printStackTrace();
            throw e;
        }

        //获取系统参数，是否开启调用SAP报检接口
        Boolean flag = isCallSAPInterface();
        log.info("flag:{}",flag);
        log.info("【定时任务-产品判检】未处理的SN按 仓库/库位/货主/货品/工厂/供应商/批次号/库存性质/品质状态分组共" + keySet.size() + "组");
        int row = 1;
        for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext(); ) {
            List<OqcInspDto> oqcInspDtoList = collect.get(iterator.next());
            OqcInspDto oqcInspDto = oqcInspDtoList.get(0);
            HashMap<String, BigDecimal> snAndQty = new HashMap<>();
            for (OqcInspDto inspDto : oqcInspDtoList) {
                snAndQty.put(inspDto.getGoodsSn(), inspDto.getQty());
            }
            oqcInspDto.setSnAndQty(snAndQty);
            Optional<BigDecimal> reduce = oqcInspDtoList.stream().filter(o -> o.getQty() != null).map(o -> o.getQty()).reduce((x, y) -> x.add(y));
            BigDecimal qty = reduce.get();
            oqcInspDto.setQty(qty); //一个Dto代表一组
            //翻牌
            try {
                row = this.flopNew(row, oqcInspDto, oqcInspDtoList);
            } catch (Exception e) {
                e.printStackTrace();
                log.warn(e.getMessage() + oqcInspDto.getInspNo());
                log.warn("【定时任务-产品判检】库存交易凭证：入库 ");
            }

            if (flag) {
                // 调QMS接口向SAP报检且回写更新检验单
                //按主档分组
                log.info("调QMS向SAP报检接口");
                ArrayList<OqcInspDto> sapCopy = new ArrayList<>();
//            oqcInspDtos.addAll(sapCopy);
                sapCopy.addAll(oqcInspDtoList);
                Map<Integer, List<OqcInspDto>> oqcInspHeaderGroup = sapCopy.stream().collect(Collectors.groupingBy(OqcInspDto::getOqcInspId));
                if (oqcInspHeaderGroup != null) {
                    log.info("map的size:{}",oqcInspHeaderGroup.size());
                    log.info("map的size:{}",oqcInspHeaderGroup.size());
                    log.info("map的size:{}",oqcInspHeaderGroup.size());
                    log.info("map的size:{}",oqcInspHeaderGroup.size());
                }

                Set<Integer> headerIds = oqcInspHeaderGroup.keySet();

                log.info("headerIds:{}",headerIds.size());
                log.info("headerIds:{}",headerIds.size());
                log.info("headerIds:{}",headerIds.size());

                for (Iterator iterator1 = headerIds.iterator(); iterator1.hasNext(); ) {
                    List<OqcInspDto> oqcInspDtoList1 = oqcInspHeaderGroup.get(iterator1.next());
                    OqcInspDto oqcInspDto1 = oqcInspDtoList1.get(0);
                    OqcInsp oqcInsp = new OqcInsp();
                    BeanUtil.copyNonNull(oqcInspDto1, oqcInsp);
                    OqcInsp o = oqcInspMapper.selectOne(new QueryWrapper<>(new OqcInsp().setOqcInspId(oqcInsp.getOqcInspId())));
                    oqcInsp.setQmsOqcId(o.getQmsOqcId());
                    oqcInsp.setPrdWo(o.getPrdWo());
                    oqcInsp.setSkuCode(o.getSkuCode());
                    oqcInsp.setFactory(o.getFactory());
                    oqcInsp.setLastStatus(o.getLastStatus());
//            int count = oqcInspDtlMapper.selectCountByOqcInspId(oqcInsp.getOqcInspId());

                    //修改抛异常逻辑
                    log.info("【报检】单号：{},数量：{}",oqcInsp.getInspNo(),oqcInspDtoList1.size());
                    Boolean reportFlag = reportCheckService.reportAndUpdateSchedule(oqcInsp, oqcInspDtoList1.size());
                    if (!reportFlag) {
                        String errMsg = "调QMS向SAP报检接口异常";
                        log.error(errMsg);
                        throw new RuntimeException(errMsg);
                    }
//                    try {
//
//                    } catch (RestStatusException e) {
//                        e.printStackTrace();
//                        log.error("调QMS向SAP报检接口异常");
//                        throw e;
//                    }
                }

            }
        }

        //变更送检单(WMS_OQC_INSP)状态：明细全部处理完，主档状态= 已处理(2)，明细只处理部分 主档状态=部分处理(1)
        HashSet<Integer> idsCannotFlopSet = new HashSet<>(idsCannotFlop);
        ArrayList<Integer> halfIds = new ArrayList<>(idsCannotFlopSet);//部分处理的主档ID
//        halfIds.stream().filter(new Predicate<Integer>() {
//            @Override
//            public boolean test(Integer integer) {
//                int i = oqcInspDtlMapper.selectCountByOqcInspId(integer); //明细行总条数
//                Goods goods = goodsService.selectBySn(i);
//                if (goods == null || goods.getGoodsId() == null || GoodsStatusEnum.CREATE.getCode() == goods.getStatus()
//                        || GoodsStatusEnum.OUT_ONLOAD.getCode() == goods.getStatus()) {
//                    return false;
//                }
//                return true;
//            }
//        })
        List<Integer> completeIds = idsAll.stream().filter(i -> !halfIds.contains(i)).collect(Collectors.toList());
        HashSet<Integer> integers = new HashSet<>(completeIds);
        completeIds = new ArrayList<>(integers);//处理完成的IDs
        if (!CollectionUtil.isEmpty(halfIds)) {
            int row1 = oqcInspMapper.updateStatusByIds(halfIds, OqcInspStatusEnum.OQC_STATUS_PART_HANDLE.getCode());
        }
        log.info("【定时任务-产品判检】变更送检单状态主档(部分处理)完成，数量" + halfIds.size());
        if (!CollectionUtil.isEmpty(completeIds)) {
            int row2 = oqcInspMapper.updateStatusByIds(completeIds, OqcInspStatusEnum.OQC_STATUS_AFTER_HANDLE.getCode());
        }
        log.info("【定时任务-产品判检】变更送检单状态主档(全部处理)完成，数量" + completeIds.size());
    }


    private int insertTrsn(int rowIndex, OqcInspDto oqcInspDto, int invUpdateFlag) throws Exception {
        TrsnDto trsnDto = new TrsnDto();
        //生成凭证号
        String NoteNo = this.basicSimpleCodeRuleApi.GenerateCode(this.NOTE_NO);
        AsnDtl asnDtl = new AsnDtl();
        log.warn("-------------------------->凭证号，出库 noteNo :{}", NoteNo);
        //表头
        TrsnHeader trsnHeader = new TrsnHeader();
        trsnHeader.setNoteNo(NoteNo);
        trsnHeader.setNoteType("T02");
        trsnHeader.setWhsId(oqcInspDto.getWhsId());
        trsnHeader.setFactory(oqcInspDto.getFactory());
        trsnHeader.setVendorId(oqcInspDto.getVendorId());
        trsnHeader.setFmDoc("/");
        trsnHeader.setDocRef1("/");
        trsnHeader.setDocRef2("/");
        trsnHeader.setDocRef3("/");
        trsnHeader.setDocRef4("/");
        trsnHeader.setDocRef5("/");
        trsnHeader.setComments("/");
        trsnHeader.setStatus(TrsnHeaderStatusEnum.CMP.code);
        trsnHeader.setCreateUser("system");
        trsnHeader.setCreateTime(new Date());
        trsnHeader.setOwner(oqcInspDto.getOwner());
        if (oqcInspDto.getInspNo() != null) {
            asnDtl = asnDtlMapper.selectOne(new QueryWrapper<>(new AsnDtl().setInspNo(oqcInspDto.getInspNo())));
        }
        if (asnDtl == null) {
            throw new Exception("------------>翻牌：生成库存交易凭证失败，找不到对应收货单号");
        }
        if (asnDtl != null) {
            trsnHeader.setFmDoc(asnDtl.getAsnNo());
        } else {
            trsnHeader.setFmDoc("/");
        }
        //            明细
        trsnDto.setTrsnHeader(trsnHeader);
        ArrayList<TrsnDtlDto> trsnDtlDtos = new ArrayList<>();
        TrsnDtlDto trsnDtlDto = new TrsnDtlDto();
        TrsnDtl trsnDtl = new TrsnDtl();
        Lotno lotno = new Lotno();
        if (oqcInspDto.getLotno() != null) {
            lotno = lotnoMapper.selectOne(new QueryWrapper<>(new Lotno().setLotno(oqcInspDto.getLotno())));
        }
        if (lotno == null) {
            log.error("【定时任务-产品判检】找不到对应批次号,SN:{},lotNo:{}", oqcInspDto.getGoodsSn(), oqcInspDto.getLotno());
        }
        trsnDtl.setNoteNo(NoteNo);
        trsnDtl.setTrsnHeaderId(trsnHeader.getTrsnHeaderId());
        trsnDtl.setQaStatus(oqcInspDto.getQaStatus());
        trsnDtl.setFmDoc(asnDtl.getAsnNo());
        trsnDtl.setFmDocLine(asnDtl.getAsnLineNo());
        trsnDtl.setInvUpdateFlag(invUpdateFlag);
        trsnDtl.setUnit(oqcInspDto.getUnit());
        trsnDtl.setNoteLineNo("1");
        trsnDtl.setLotAttr1(lotno.getLotAttr1());
        trsnDtl.setLotAttr2(lotno.getLotAttr2());
        trsnDtl.setLotAttr3(lotno.getLotAttr3());
        trsnDtl.setLotAttr4(lotno.getLotAttr4());
        trsnDtl.setLotAttr5(lotno.getLotAttr5());
        trsnDtl.setLotAttr6(lotno.getLotAttr6());
        trsnDtl.setLotAttr7(lotno.getLotAttr7());
        trsnDtl.setLotAttr8(lotno.getLotAttr8());
        trsnDtl.setLotAttr9(lotno.getLotAttr9());
        trsnDtl.setLotAttr10(lotno.getLotAttr10());
        trsnDtl.setLotAttr11(lotno.getLotAttr11());
        trsnDtl.setLotAttr12(lotno.getLotAttr12());
        trsnDtl.setQty(oqcInspDto.getQty());
        trsnDtl.setInvType("X");
        trsnDtl.setStatus(TrsnDtlStatusEnum.CMP.code);
        trsnDtl.setComments("/");
        trsnDtl.setSkuId(oqcInspDto.getSkuId());
        trsnDtl.setCreateTime(new Date());
        trsnDtl.setCreateUser("system");
        //明细-goods
        ArrayList<TrsnDtlGoods> dtlGoodsArrayList = new ArrayList<>();
        Map<String, BigDecimal> snAndQty = oqcInspDto.getSnAndQty();
        log.info("【翻牌】-> 插入库存交易凭证,{},类型:{}", snAndQty, invUpdateFlag);
        if (snAndQty != null && snAndQty.size() != 0) {
            snAndQty.forEach(new BiConsumer<String, BigDecimal>() {
                @Override
                public void accept(String sn, BigDecimal qty) {
                    TrsnDtlGoods trsnDtlGoods = new TrsnDtlGoods();
                    trsnDtlGoods.setTrsnDtlId(trsnDtl.getTrsnDtlId());
                    trsnDtlGoods.setQaStatus(oqcInspDto.getQaStatus());
                    trsnDtlGoods.setGoodsId(oqcInspDto.getGoodsId());
                    trsnDtlGoods.setGoodsSn(sn);
                    trsnDtlGoods.setQty(qty);
                    trsnDtlGoods.setStatus(TrsnDtlGoodsStatusEnum.RECEIVED.getCode());
                    trsnDtlGoods.setCreateTime(new Date());
                    trsnDtlGoods.setCreateUser("system");
                    dtlGoodsArrayList.add(trsnDtlGoods);
                }
            });
        } else {
            TrsnDtlGoods trsnDtlGoods = new TrsnDtlGoods();
            trsnDtlGoods.setTrsnDtlId(trsnDtl.getTrsnDtlId());
            trsnDtlGoods.setQaStatus(oqcInspDto.getQaStatus());
            trsnDtlGoods.setGoodsId(oqcInspDto.getGoodsId());
            trsnDtlGoods.setGoodsSn(oqcInspDto.getGoodsSn());
            trsnDtlGoods.setQty(oqcInspDto.getQty());
            trsnDtlGoods.setStatus(TrsnDtlGoodsStatusEnum.RECEIVED.getCode());
            trsnDtlGoods.setCreateTime(new Date());
            trsnDtlGoods.setCreateUser("system");
            dtlGoodsArrayList.add(trsnDtlGoods);
        }
        trsnDtlDto.setTrsnDtl(trsnDtl);
        trsnDtlDto.setTrsnDtlGoodsList(dtlGoodsArrayList);
        trsnDtlDtos.add(trsnDtlDto);
        trsnDto.setTrsnDtlDtoList(trsnDtlDtos);
        trsnService.addNewOne(trsnDto);
        rowIndex++;
        return rowIndex;
    }

    *//**
     * 获取系统参数，是否开启调用SAP报检接口
     *
     * @return
     *//*
    public Boolean isCallSAPInterface() {
        ParamDetailDTO Is_callSAPInterface = basicParamApi.getByCode("Is_callSAPInterface");
        if (Is_callSAPInterface != null && Is_callSAPInterface.getSystemParamValue() != null) {
            String systemParamValue = Is_callSAPInterface.getSystemParamValue();
            if (systemParamValue.equals("true")) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    *//**
     * 翻牌(变更库位库存)（新）
     *//*
    public int flopNew(int row, OqcInspDto oqcInspDto, List<OqcInspDto> oqcInspDtoList) throws Exception {
//        //变更货品清单(WMS_GOODS)的品质状态，待检—>合格/不合格；合格—>不合格；不合格—>合格。
//        //调【更新货品】、【生成SN操作日志】服务
//        Goods goods = new Goods();
//        BeanUtil.copyNonNull(oqcInspDto, goods);
//        Goods goodsRecord = goodsService.selectBySn(oqcInspDto.getGoodsSn()); //为了记录旧的goods日志，再查一次
//        goodsService.updateGoods(null, goods);
//        GoodsLog goodsLog = new GoodsLog();
//        goodsLog.setGoodsSn(goods.getGoodsSn());
//        goodsLog.setOpCode("翻牌"+goodsRecord.getGoodsSn());
//        goodsLog.setReason("翻牌");
//        goodsLog.setOperator("定时任务-产品判检");
//        goodsLog.setUpdateContent("未更新的：" + JSON.toJSONString(goodsRecord) + ",更新后的：" + JSON.toJSONString(goods));
//        goodsLog.setCreateUser("system");
//        goodsLog.setCreateTime(new Date());
//        goodsService.insertGoodsLog(goodsLog);
        //变更库位库存(WMS_INV_LOC)，变更的是库存品质状态栏位。调【库存更新】、【生成库存日志】服务,调两次
        try {
            UpdateInventoryInputDto old = new UpdateInventoryInputDto();
            old.setLocId(oqcInspDto.getLocId());//设置库位
            old.setQty(oqcInspDto.getQty().negate());
            old.setPreInQty(BigDecimal.ZERO);
            old.setPreOutQty(BigDecimal.ZERO);
            old.setHoldQty(BigDecimal.ZERO);
            old.setAlocQty(BigDecimal.ZERO);
            old.setPreAlocQty(BigDecimal.ZERO);
            old.setOperator("system");
            old.setWhsId(oqcInspDto.getWhsId());
            old.setOwner(oqcInspDto.getOwner());
            old.setSkuId(oqcInspDto.getSkuId());
            old.setFactory(oqcInspDto.getFactory());
            old.setVendorId(oqcInspDto.getVendorId());
            old.setLotNo(oqcInspDto.getLotno());
            old.setInvAttr(oqcInspDto.getInvAttr());
            old.setQaStatus(oqcInspDto.getQaStatusOld());
            old.setUnit(oqcInspDto.getUnit());
            UpdateInventoryInputDto newDto = new UpdateInventoryInputDto();
            BeanUtil.copyNonNull(old, newDto);
            newDto.setLocId(oqcInspDto.getLocId());//设置库位
            newDto.setQty(oqcInspDto.getQty());
            newDto.setQaStatus(oqcInspDto.getQaStatus());
            InvTrans log = new InvTrans();
            log.setOpCode(OpCodeEnum.INSPECTIONRESULT.getDescription());
            log.setDocType(" ");//TODO
            log.setDocNo(" ");
            log.setDocLineNo(" ");
            log.setTrsnNo(" ");
            log.setTrsnLineNo(String.valueOf(" "));
            log.setStatus(InvTransStatusEnum.FINISHED.getCode());
            log.setTransTime(new Date());
            log.setOperator("system");
            log.setTransQty(oqcInspDto.getQty());
            log.setCreateTime(new Date());
            log.setCreateUser("system");
            inventoryService.inventoryIn(newDto, log);
        } catch (Exception e) {
            log.info("【定时任务-产品判检】翻牌-变更库位库存失败，类型：入库，{}", oqcInspDto.getInspNo());
            e.printStackTrace();
        }


        //	插入库存交易凭证(WMS_TRSN_HEADER)、库存交易记录明细(WMS_TRSN_DTL)记录, WMS_TRSN_DTL_GOODS
        row = insertTrsn(row, oqcInspDto, 1);

        //变更送检单明细(WMS_OQC_INSP_DTL)状态=已处理(1)
        for (OqcInspDto inspDto : oqcInspDtoList) {
            oqcInspDtlMapper.updateStatusById(inspDto.getOqcInspDtlId(), OqcInspDtlStatusEnum.OQCDTL_STATUS_AFTER_HANDLE.getCode());
        }
        log.info("【定时任务-产品判检】按维度组变更送检单明细状态完成,数量" + oqcInspDtoList.size());
        return row;
    }


    *//**
     * 合并的key
     * 根据“库位、货主、货品、工厂、供应商、批次号、库存性质、品质状态”分组统计数量
     *
     * @param
     * @return
     *//*
    public String spliceGroupingKey(OqcInspDto oqcInspDto) {
        return oqcInspDto.getLocId() + "#" + oqcInspDto.getOwner() + "#" + oqcInspDto.getSkuId() + "#" +
                oqcInspDto.getFactory() + "#" + oqcInspDto.getVendorId() + "#" + oqcInspDto.getLotno() +
                "#" + oqcInspDto.getInvAttr() + "#" + oqcInspDto.getQaStatus() + "#" + oqcInspDto.getWhsId();
    }*/
}
